package com.indra.archetypeweb.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.indra.archetypeweb.dao.product.ProductDao;
import com.indra.archetypeweb.dto.BookDTO;
import com.indra.archetypeweb.dto.CDDTO;
import com.indra.archetypeweb.dto.ProductDTO;
import com.indra.archetypeweb.entity.book.Book;
import com.indra.archetypeweb.entity.disc.Disc;
import com.indra.archetypeweb.entity.product.Product;
import com.indra.archetypeweb.controller.AdminController;
import com.indra.archetypeweb.utils.constants.ProductType;

public class ProductServiceImpl implements ProductService {
	
	@Autowired
	private ProductDao productDao;
	
	private static final Logger LOG = LoggerFactory
			.getLogger(ProductServiceImpl.class);

	@Override
	public List<ProductDTO> loadProducts() {
		List<ProductDTO> listProductDTO = new ArrayList<ProductDTO>();
		ProductDTO productDTO = new ProductDTO();
		try{
			List<Product> listProduct = productDao.findAll();
			for(Product product : listProduct){
				productDTO.setId(product.getId());
				productDTO.setName(product.getName());
				productDTO.setPrice(product.getPrice());
				productDTO.setDescription(product.getDescription());
				productDTO.setDateAdded(product.getDateAdded());
				productDTO.setDateDeleted(product.getDateDeleted());
				switch (product.getProductType()){
					case 0:
						productDTO.setProductType(ProductType.BOOK);
						break;
					case 1:
						productDTO.setProductType(ProductType.CD);
						break;
				}
				listProductDTO.add(productDTO);
			}
		} catch(Exception e){
			LOG.error("Error en la busqueda de productos", e);
		}
		return listProductDTO;
	}

	@Override
	public List<ProductDTO> loadProductsByProductType(ProductType productType) {
		List<ProductDTO> listProductDTO = new ArrayList<ProductDTO>();
		ProductDTO productDTO = new ProductDTO();
		int type = 0;
		try{
			switch (productType){
			case BOOK:
				type = 0;
				break;
			case CD:
				type = 1;
				break;
			}
			List<Product> listProduct = productDao.findByType(type);
			for(Product product : listProduct){
				productDTO.setId(product.getId());
				productDTO.setName(product.getName());
				productDTO.setPrice(product.getPrice());
				productDTO.setDescription(product.getDescription());
				productDTO.setDateAdded(product.getDateAdded());
				productDTO.setDateDeleted(product.getDateDeleted());
				productDTO.setProductType(productType);
				
				listProductDTO.add(productDTO);
			}
		} catch(Exception e){
			LOG.error("Error en la busqueda de productos", e);
		}
		return listProductDTO;
	}

	@Override
	public List<ProductDTO> loadProductsByCathegory(String cathegory) {
		/*List<ProductDTO> listProductDTO = null;
		try{
			if(cathegory != null && !cathegory.equals("")){
				listProduct = productDao.findProductsByCathegory(cathegory);
			}
		} catch(Exception e){
			LOG.error("Error en la busqueda de productos", e);
		}
		return listProductDTO;*/
		return null;
	}

	@Override
	public ProductDTO loadProduct(ProductDTO productDTO) {
		/*try{
			if(productDTO != null && productDTO.getId() != null && !productDTO.getId().equals("")){
				switch (productDTO.getProductType()){
					case CD:
						Disc cd = productDao.findCDByIdProduct(productDTO.getId());
						CDDTO cdDTO = new CDDTO();
						cdDTO.setCompany(cd);
						cdDTO.setGroup(cd);
						cdDTO.setIdCd(cd);
						productDTO.setCd(cdDTO);
						break;
					case BOOK:
						Book book = productDao.findBookByIdProduct(productDTO.getId());
						BookDTO bookDTO = new BookDTO();
						bookDTO.setCompany(book);
						bookDTO.setGroup(book);
						bookDTO.setIdCd(book);
						productDTO.setBook(bookDTO);
						break;
				}
			}
		} catch(Exception e){
			LOG.error("Error en la busqueda de producto", e);
		}
		return productDTO;*/
		return null;
	}
	
	

}
